package reflect;

import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {
        Person p = new Person();
        p.sayHello();


        Scanner scanner = new Scanner(System.in);
        System.out.println("输入类名");
        String classname = scanner.nextLine();
        System.out.println("输入方法名");
        String mthodname = scanner.nextLine();
        /*实例化*/
        Class cls = Class.forName(classname);
        Object obj = cls.newInstance();
        /*调用方法
        * 1获取待调用方法的方法对象*/
        Method method = cls.getMethod(mthodname);/*至少传入一个方法名*/
        /*通过方法对象来调用次方法*/
        method.invoke(obj);
    }
}
